package com.lemon.smartsafetynew.ui.activity.record;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lemon.android.http.HttpUtils;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafety.utils.common.LemonFileHelper;
import com.lemon.smartsafetynew.entity.FileInfo;
import com.lemon.smartsafetynew.entity.common.DateTypeEnum;
import com.lemon.smartsafetynew.entity.event.incident.MediaEvent;
import com.lemon.smartsafetynew.entity.patrol.SafePatrolMediaVo;
import com.lemon.smartsafetynew.entity.record.EquipVo;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafetynew.ui.adapter.incident.IncidentAttachmentAdapter;
import com.lemon.smartsafetynew.ui.adapter.record.FilesAdapter;
import com.lemon.smartsafetynew.util.common.CommonUtil;
import com.lemon.smartsafetynew.util.constants.KeyConstants;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.finalteam.galleryfinal.model.PhotoInfo;

/**
 * 一企一档特种设备详情
 * Created by JiangAn on 2018/5/28.
 */

public class RecordEquipmentDetailActivity extends SwipeBackActivity implements RecyclerTClickListener<FileInfo> {

    @Bind(R.id.iv_return)
    ImageView ivReturn;
    @Bind(R.id.tv_equipment_name)
    TextView tvEquipmentName;
    @Bind(R.id.tv_equip_category)
    TextView tvEquipCategory;
    @Bind(R.id.tv_equip_type)
    TextView tvEquipType;
    @Bind(R.id.tv_register_number)
    TextView tvRegisterNumber;
    @Bind(R.id.tv_out_company_code)
    TextView tvOutCompanyCode;
    @Bind(R.id.tv_user_vercificite)
    TextView tvUserVercificite;
    @Bind(R.id.tv_out_company_type)
    TextView tvOutCompanyType;
    @Bind(R.id.tv_user_position)
    TextView tvUserPosition;
    @Bind(R.id.tv_emergency_number)
    TextView tvEmergencyNumber;
    @Bind(R.id.tv_main_params)
    TextView tvMainParams;
    @Bind(R.id.tv_equipment_function)
    TextView tvEquipmentFunction;
    @Bind(R.id.tv_check_date)
    TextView tvCheckDate;
    @Bind(R.id.tv_validity_date)
    TextView tvValidityDate;
    @Bind(R.id.tv_equipment_type)
    TextView tvEquipmentType;
    @Bind(R.id.tv_equipment_to_position)
    TextView tvEquipmentToPosition;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.tv_contact_people)
    TextView tvContactPeople;
    @Bind(R.id.tv_contact_telephone)
    TextView tvContactTelephone;
    @Bind(R.id.tv_space_remark)
    TextView tvSpaceRemark;
    @Bind(R.id.ryHighFile)
    RecyclerView ryHighFile;
    @Bind(R.id.tv_file_none)
    TextView tvFileNone;

    private static EquipVo data;
    private Boolean isShowReturn = false;
    private IncidentAttachmentAdapter mMediaAdapter;

    @Override
    protected void initTitle() {
        this.setBarTitle(getString(R.string.equipment_name));
    }

    @Override
    protected void getBundleArgs() {
        super.getBundleArgs();

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            data = (EquipVo) bundle.getSerializable(KeyConstants.KEY_RECORD_EQUIPMENT_VO);
            isShowReturn = bundle.getBoolean("isShowReturn");
        }
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(RecordEquipmentDetailActivity.this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_record_equipment_detail;
    }

    @Override
    protected void initViews() {
        super.initViews();
        if (isShowReturn) {
            ivReturn.setVisibility(View.VISIBLE);
        } else {
            ivReturn.setVisibility(View.INVISIBLE);
        }
        initEquipment();
    }

    private void initEquipment() {
        if (data != null) {
            tvEquipmentName.setText(CommonUtil.avoidNullString(data.getData_true_name()));
            tvEquipCategory.setText(CommonUtil.avoidNullString(data.getData_name_big()));
            tvEquipType.setText(CommonUtil.avoidNullString(data.getData_name_small()));
            tvRegisterNumber.setText(CommonUtil.avoidNullString(data.getRegister_code()));
            tvOutCompanyCode.setText(CommonUtil.avoidNullString(data.getFactory_number()));
            tvUserVercificite.setText(CommonUtil.avoidNullString(data.getUsed_code()));
            tvOutCompanyType.setText(CommonUtil.avoidNullString(data.getModel()));
            tvUserPosition.setText(CommonUtil.avoidNullString(data.getUsed_part()));
            tvEmergencyNumber.setText(CommonUtil.avoidNullString(data.getCount()));
            tvMainParams.setText(CommonUtil.avoidNullString(data.getMain_parameter()));
            tvEquipmentFunction.setText(CommonUtil.avoidNullString(data.getWorking()));
            tvCheckDate.setText(CommonUtil.secondToTimeStr(data.getCheck_date(), DateTypeEnum.DAY));
            tvValidityDate.setText(CommonUtil.secondToTimeStr(data.getRecheck_date(), DateTypeEnum.DAY));
            tvEquipmentType.setText(CommonUtil.avoidNullString(data.getCheck_company()));
            tvEquipmentToPosition.setText(CommonUtil.avoidNullString(data.getPlace()));
            tvContactPeople.setText(CommonUtil.avoidNullString(data.getLinkman()));
            tvContactTelephone.setText(CommonUtil.avoidNullString(data.getPhone()));
            tvSpaceRemark.setText(CommonUtil.avoidNullString(data.getRemark()));
            initAdapter();
        }
    }

    private void initAdapter() {
        if (!TextUtils.isEmpty(data.getPic_url())) {
            List<String> idList = Arrays.asList(data.getPic_url().split(","));
            List<SafePatrolMediaVo> photoVo = new ArrayList<>();
            for (int i = 0; i < idList.size(); i++) {
                SafePatrolMediaVo safePatrolMediaVo = new SafePatrolMediaVo();
                safePatrolMediaVo.setData_url(idList.get(i));
                photoVo.add(safePatrolMediaVo);
            }
            GridLayoutManager gridLayoutManager = new GridLayoutManager(RecordEquipmentDetailActivity.this, 3);
            recyclerView.setLayoutManager(gridLayoutManager);
            recyclerView.setNestedScrollingEnabled(false);
            mMediaAdapter = new IncidentAttachmentAdapter(photoVo, RecordEquipmentDetailActivity.class.getName());
            recyclerView.setAdapter(mMediaAdapter);
        }
        if (!TextUtils.isEmpty(data.getCheck_file_url())) {
            tvFileNone.setVisibility(View.GONE);
            ryHighFile.setVisibility(View.VISIBLE);
            List<String> list = Arrays.asList(data.getCheck_file_url().split(","));//根据逗号分隔转化为list
            List<FileInfo> fileList = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                FileInfo fileinfo = new FileInfo();
                fileinfo.setName("附件一");
                fileinfo.setUrl(HttpUtils.formatFilesUrl(data.getCheck_file_url()));
                fileList.add(fileinfo);
            }
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(RecordEquipmentDetailActivity.this);
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            ryHighFile.setLayoutManager(linearLayoutManager);
            ryHighFile.setNestedScrollingEnabled(false);
            FilesAdapter mAdapter = new FilesAdapter(fileList, this);
            ryHighFile.setAdapter(mAdapter);
        }else{
            tvFileNone.setVisibility(View.VISIBLE);
            ryHighFile.setVisibility(View.GONE);
        }
    }

    /**
     * 显示图片和音频详情
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMediaEvent(MediaEvent event) {

        if (RecordEquipmentDetailActivity.class.getName().equals(event.getTag())) {

            SafePatrolMediaVo data = event.getMediaVos().get(event.getPosition());

            if (LemonFileHelper.isImageUrl(data.getData_url())) {
                List<SafePatrolMediaVo> mediaVos = event.getMediaVos();
                List<PhotoInfo> photos = new ArrayList<>();

                int j = 0;
                int currentItem = 0;

                for (int i = 0; i < mediaVos.size(); i++) {
                    if (LemonFileHelper.isImageUrl(mediaVos.get(i).getData_url())) {

                        if (data.getData_url().equals(mediaVos.get(i).getData_url())) {
                            currentItem = j;
                        }

                        PhotoInfo photoInfo = new PhotoInfo();
                        photoInfo.setPhotoId(j);
                        photoInfo.setPhotoPath(HttpUtils.formatFilesUrl(mediaVos.get(i).getData_url()));
                        photos.add(photoInfo);
                        j++;
                    }
                }

                if (photos != null && photos.size() != 0) {
                    UIHelper.showPhotoView(this, photos, currentItem);
                }
            } else if (LemonFileHelper.isVoiceUrl(data.getData_url())) {
                playVoice(HttpUtils.formatFilesUrl(data.getData_url()));
            }

        }
    }

    @Override
    protected void initListener() {
        super.initListener();
        ivReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppManager.getAppManager().finishActivity(RecordEquipmentDetailActivity.this);
                AppManager.getAppManager().finishActivity(RecordEquipmentActivity.class);
            }
        });
    }

    @Override
    public void onItemClick(FileInfo data, int position) {
        downloadAndShowFile(data.getUrl(), data.getName(), this, getResources().getString(R.string.record_material));
    }

}




